
const SECRET_KEY = '此处填写第三方平台中控服务参数加密secret';
module.exports = {
	async requestApi(params, key = '') {
	
	    let {url, endpoint, componentAppId, appId, method, query, post} = params
	    key = key || SECRET_KEY
	
	    if( !key ) throw new Error('没有配置参数加密字串')
	    if( !url ) throw new Error('缺少参数URL')
	    if( !componentAppId ) throw new Error('缺少第三方平台AppID')
	    if( !appId ) throw new Error('缺少授权给第三方平台的公众号/小程序AppID')
	    if( ['POST','GET'].includes(`${method}`.toUpperCase() == false ) ) throw new Error('请求method无效')
	
	    const data = {
	        endpoint,
	        componentAppId,
	        appId,
	        method,
	    }
	
	    //设置GET参数
	    query && Object.keys(query).length && (data.query = query)
	    //设置POST数据
	    method == 'POST' && post && Object.keys(post).length && (data.post = post)
	
	    //将数据格式化为JSON字符串
	    const postJson = JSON.stringify(data)
	
	    //拼合key和JSON字符串
	    const signString = key + postJson
	    
	    //生成拼合字符串的md5值
	    const sign = require('crypto').createHash('md5').update(signString).digest('hex')
	
	    //将md5值作为URL GET 参数 sign
	    url = `${url}/index?sign=${sign}`
		
	    return new Promise((resolve, reject)=>{
			uni.request({
				url,
			    data,
				contentType:'json',
				method:'POST',
				dataType:'json',
				success(e){ 
					console.log('success', e)
					resolve(e)
				},
				fail(e){ 
					console.log('error', e)
					reject(e)
				}
			})
		})
	},
	async getToken(params, key = '') {
	
	    let {url, endpoint, componentAppId, appId, method, query, post} = params
	    key = key || SECRET_KEY
	
	    if( !key ) throw new Error('没有配置参数加密字串')
	    if( !url ) throw new Error('缺少参数URL')
	    if( !componentAppId ) throw new Error('缺少第三方平台AppID')
	
	    const data = {componentAppId}
		appId && (data.appId = appId)
	
	    //将数据格式化为JSON字符串
	    const postJson = JSON.stringify(data)
	
	    //拼合key和JSON字符串
	    const signString = key + postJson
	    
	    //生成拼合字符串的md5值
	    const sign = require('crypto').createHash('md5').update(signString).digest('hex')
	
	    //将md5值作为URL GET 参数 sign
	    url = `${url}getToken?sign=${sign}`
		
	    return new Promise((resolve, reject)=>{
			uni.request({
				url,
			    data,
				contentType:'json',
				method:'POST',
				dataType:'json',
				success(e){ 
					console.log('success', e)
					resolve(e)
				},
				fail(e){ 
					console.log('error', e)
					reject(e)
				}
			})
		})
	},
	async updateToken(params, key = '') {
	
	    let {url, endpoint, componentAppId, appId, method, query, post} = params
	    key = key || SECRET_KEY
	
	    if( !key ) throw new Error('没有配置参数加密字串')
	    if( !url ) throw new Error('缺少参数URL')
	    if( !componentAppId ) throw new Error('缺少第三方平台AppID')
	
	    const data = {componentAppId}
		appId && (data.appId = appId)
	
	    //将数据格式化为JSON字符串
	    const postJson = JSON.stringify(data)
	
	    //拼合key和JSON字符串
	    const signString = key + postJson
	    
	    //生成拼合字符串的md5值
	    const sign = require('crypto').createHash('md5').update(signString).digest('hex')
	
	    //将md5值作为URL GET 参数 sign
	    url = `${url}updateToken?sign=${sign}`
		
	    return new Promise((resolve, reject)=>{
			uni.request({
				url,
			    data,
				contentType:'json',
				method:'POST',
				dataType:'json',
				success(e){ 
					console.log('success', e)
					resolve(e)
				},
				fail(e){ 
					console.log('error', e)
					reject(e)
				}
			})
		})
	}
}